<?php
	header('content-type:text/html;charset=utf-8');
	
	$db21 = array(); //3.21的服务器结果集
	$fwq = array(); //点胶服务器的结果集
	
	$i = 0;
	
	$my21 = null; //全局一下
	
	$dbName = "sqlsrv:Server=172.31.3.12,1433;Database=UFDATA_001_2017";
	$dbUser = "user01";
	$dbPassword = "TDled2018@";
	
	//u8结果集函数
	function fruit($ID, $CM, $PH, $XCQTY, $DFH, $KYQTY, $CS, $CHBM, $FGBM, $CSBM, $BBBM) {
		global $fwq;
		$fwq[$ID] = array($CM, $PH, $XCQTY, $DFH, $KYQTY, $CS, $CHBM, $FGBM, $CSBM, $BBBM);
		// print_r("<br>");
		// print_r($ID);
		// print_r("<br>");
		// print_r(json_encode($fwq[$ID]));
	}
	
	//321结果集函数
	function db321($ID, $CM, $PH, $XCQTY, $DFH, $KYQTY, $CS, $CHBM, $FGBM, $CSBM, $BBBM) {
		global $db21;
		$db21[$ID] = array($CM, $PH, $XCQTY, $DFH, $KYQTY, $CS, $CHBM, $FGBM, $CSBM, $BBBM);
		// print_r("<br>");
		// print_r($ID);
		// print_r("<br>");
		// print_r(json_encode($db21[$ID]));
	}
	
	try {
		global $my21; //全局一下
		$my21 = new PDO("mysql:host=127.0.0.1;port=3306;dbname=tddb;","root","tdled2018", array(PDO::ATTR_PERSISTENT => true));
		$my21->query("set character set 'utf8'");//读库
		$my21->query("set names 'utf8'");//写库
		
		$SQL1 = "
			SELECT
				ID,
				`仓库编码` AS CM,
				`批号` AS PH,
				`现存数量` AS XCQTY,
				`待发货数量` AS DFH,
				`可用数量` AS KYQTY,
				`参数1` AS CS,
				`存货编码` AS CHBM,
				`分光编码` AS FGBM,
				`参数编码` AS CSBM,
				`版本编码` AS BBBM
			FROM
				`led现存量`
			;
		";
		
		$res=$my21->query($SQL1);//准备查询语句
		
		$res->fetchAll(PDO::FETCH_FUNC, "db321");
		
		//print_r($HSQL . $DSQL . ";");
		//print_r("<br>----------<br>");
		//var_dump($res);
		
		$u8 = new PDO($dbName, $dbUser, $dbPassword, array(PDO::ATTR_PERSISTENT => true));
		
		$sql = "
			SELECT
				t1.cWhCode + t1.cBatch + t1.cFree2 + t1.cFree3 + t1.cFree1 AS ID,
				t1.cWhCode AS CM,
				t1.cBatch AS PH,
				t1.iQuantity AS XCQTY,
				t1.fOutQuantity AS DFH,
				t1.iQuantity - t1.fOutQuantity AS KYQTY,
				CASE WHEN t2.LEDPar1 IS NULL THEN t3.LEDPar1 ELSE t2.LEDPar1 END AS 'CS',
				t1.cInvCode AS CHBM,
				t1.cFree2 AS FGBM,
				t1.cFree3 AS CSBM,
				t1.cFree1 AS BBBM
			FROM
				CurrentStock t1
				LEFT JOIN HY_DZ_LED_PARAMETERM t2 ON t1.cinvcode = t2.cinvcode
				AND t1.cfree2 = t2.bincode
				AND t1.cFree3= t2.id
				LEFT JOIN HY_DZ_LED_PARAMETERM_888 AS t3 ON t1.cinvcode = t3.cinvcode
				AND t1.cfree2 = t3.bincode
				AND t1.cFree3 = t3.ID
			WHERE
				t1.cwhcode= 'D02G'
			;
		";
		
		$res = $u8->query($sql);
		$res->fetchAll(PDO::FETCH_FUNC, "fruit");
		
		//echo "The connection is successful!<br/>";
	} catch (PDOException $e) {
		die ("Error!: " . $e->getMessage() . "<br/>");
	}
	
	//对比2
	function myfunction($a,$b)
	{
		$insr = array();
		$upd = array();
		foreach($a as $k => $v)
		{
			if(array_key_exists($k,$b))
			{
				if($a[$k][2] == $b[$k][2] && $a[$k][4] == $b[$k][4] && $a[$k][5] == $b[$k][5])
				{
					
				}
				else
				{
					$upd[$k] = $a[$k];
				}
			}
			else
			{
				$insr[$k] = $a[$k];
			}
		}
		return array('0' => $insr, '1' => $upd);
	}
	
	$result = myfunction($fwq,$db21);
	// print_r(json_encode($result));
	$HSQL = "INSERT INTO `led现存量` (`ID`, `仓库编码`, `批号`, `现存数量`, `待发货数量`, `可用数量`, `参数1`, `存货编码`, `分光编码`, `参数编码`, `版本编码`) VALUES ";
	$DSQL = "";
	
	foreach($result['0'] as $k => $v)
	{
		//加入3000行运行1次的判断
		global $i; //全局一下
		$i += 1;
		global $DSQL; //全局
		
		if($DSQL != "")
		{
			$DSQL .= ",('" . $k ."','" . $v[0] ."','" . $v[1] . "','" . $v[2] . "','" . $v[3] . "','" . $v[4] . "','" . $v[5] . "','" . $v[6] . "','" . $v[7] . "','" . $v[8] ."','" . $v[9] ."')";
		}
		else
		{
			$DSQL = " ('" . $k ."','" . $v[0] ."','" . $v[1] . "','" . $v[2] . "','" . $v[3] . "','" . $v[4] . "','" . $v[5] . "','" . $v[6] . "','" . $v[7] . "','" . $v[8] ."','" . $v[9] ."')";
		}
		
		if($i % 3000 == 0)
		{
			global $my21; //全局一下
			global $DSQL; //全局
			$res=$my21->query($HSQL . $DSQL . ";");//准备查询语句
			$DSQL = ""; //运行后清空
			//print_r("<br>----------<br>");
			//var_dump($res);
		}
	}
	
	$res = $my21 -> query($HSQL . $DSQL . ";");//准备查询语句
	//print_r($HSQL . $DSQL . ";");
	//print_r("<br>----------<br>");
	//var_dump($res);
	
	$SQL = "";
	$i = 0;
	foreach($result['1'] as $k => $v)
	{
		//加入3000行运行1次的判断
		global $i; //全局一下
		$i += 1;
		
		$SQL .= "UPDATE `led现存量` SET `现存数量`='" . $v[2] . "', `待发货数量`='" . $v[3] . "', `可用数量`='" . $v[4] . "' WHERE `ID`='" . $k . "';";
		
		if($i % 3000 == 0)
		{
			$res=$my21->query($SQL);//准备查询语句
			$SQL = ""; //运行后清空
			//print_r("<br>----------<br>");
			//var_dump($res);
		}
	}
	
	$res=$my21->query($SQL);//准备查询语句
	//print_r("<br>----------<br>");
	//print_r($SQL);
	//var_dump($res);
?>